<?php
/**
 * 算力套餐日收益 每天凌晨1点执行定时任务.
 * User: cjj
 * Date: 2019/10/8
 * Time: 16:07
 */

if (!defined("APP_PATH")) {
    define('APP_PATH', __DIR__ . '/application/');
    // 加载框架引导文件
    require __DIR__ . '/thinkphp/start.php';
}

echo '开始执行时间：'.date('Y-m-d H:i:s')."\r\n";
//今天的开始时间
$stime = strtotime(date('Y-m-d'));

//首先拿1000条今天还没有结算收益的数据
// 暂时注释对今天的判断  ->where('updatetime','<',$stime) 暂时去掉
$data = db('tc_order')->where('duetime','>=',time())->where('price_d','>',0)->where('status',0)->where('days_d','>',0)->field('id,uid,username,tcid,price_d,money')->limit(0,1000)->select();
//获取所有等级的算力套餐信息
$tcinfo = db('tc_manage')->where('id','>',0)->column('id,level,per,name_cn,name_en','id');

//获取usdt转fc的比例
$bl = config('site.exchange_fc_rate');
$bl2 = key($bl);
$bl3 = $bl[$bl2];
$cper = $bl3 / $bl2;

//获取表前缀
$prefix = config('database.prefix');
$db = new \think\Db();
while($data){
    try {
        //开启事务
        $db::startTrans();
        //用户信息
        $user = null;
        $tc = null;
        $log = null;
        $tclog = null;

        $bonus = null;
        $tm = time();
        foreach ($data as $val) {
            //获取对应算力套餐的日收益比例
            $per = isset($tcinfo[$val['tcid']]) ? $tcinfo[$val['tcid']]['per'] : 0;
            $award = 0;
            if ($per > 0) {
                $award = $val['price_d'] * $per / 100;
                $credit1 = $award * $cper;
                if ($credit1 > 0) {
                    $user[] = 'update ' . $prefix . 'user set credit1 = credit1+' . $credit1 . ' where id = ' . $val['uid'];
                    $log[] = [
                        'username'   => $val['username'],
                        'type'       => 'credit1',
                        'project'    => 'tc',
                        'num'        => $credit1,
                        'remark'     => '算力套餐: [' . $tcinfo[$val['tcid']]['name_cn'] . ']结算日收益，增加' . $credit1 . config('site.credit1_text'),
                        'remark_en'  => 'Arithmetic Package [' . $tcinfo[$val['tcid']]['name_en'] . '] Increase in Settlement Date Income by ' . $credit1 . config('site.credit1_text'),
                        'createtime' => $tm,
                        'updatetime' => $tm
                    ];
                    $tclog[] = [
                        'uid' => $val['uid'],
                        'tc_order' => $val['id'],
                        'tcid' => $val['tcid'],
                        'num' => $credit1,
                        'total_num' => $credit1 + $val['money'],
                        'createtime' => $tm
                    ];
                    $tc[] = 'update ' . $prefix . 'tc_order set days_d = days_d - 1,money = money + ' . $credit1 . ',updatetime = ' . time() . ' where id = ' . $val['id'];

                    //社群算力
                    $bonus[] = [
                        'money' => $credit1,
                        'id' => $val['uid']
                    ];

                }
            }
        }

        //批量处理
        if ($user) {
            db()->batchQuery($user);
            $user = null;
        }
        if ($tc) {
            db()->batchQuery($tc);
            $tc = null;
        }
        if ($log) {
            db('cc_detail_log')->insertAll($log);
            $log = null;
        }
        if ($tclog) {
            db('tc_profit_log')->insertAll($tclog);
        }
        //触发社群算力
        if ($bonus) {
            \app\common\core\Procevent::dsell_event($bonus,'sqsl');
            $bonus = null;
        }

        //提交事务
        $db::commit();

        $data = db('tc_order')->where('duetime', '>=', time())->where('price_d','>',0)->where('status',0)->where('updatetime', '<', $stime)->where('days_d', '>', 0)->field('id,uid,username,tcid,price_d')->limit(0, 1000)->select();
    } catch (\Exception $e) {
        //事务回滚
        $db::rollback();
        //记录日志
        plog('套餐日收益结算',$e->getMessage());
        //跳出循环
        break;
    }
}
echo '执行结束时间：'.date('Y-m-d H:i:s')."\r\n";


